package com.obrempon.credit.market.ui.activity

import android.graphics.Typeface
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.View
import com.obrempon.credit.market.R
import com.obrempon.credit.market.common.CommonActivity
import com.obrempon.credit.market.configs.ObremponApiPath
import com.obrempon.credit.market.configs.ObremponApiUrl
import com.obrempon.credit.market.configs.ObremponApiUrl.addCommonParams
import com.obrempon.credit.market.databinding.ObremponCreditRejectedActBinding
import com.obrempon.credit.market.event.ObremponEventPointType
import com.obrempon.credit.market.request.IHttpCallback
import com.obrempon.credit.market.ui.dialog.ObremponUserDataClearDialog
import com.obrempon.credit.market.utils.helper.ObremponConfigsHelper
import com.obrempon.credit.market.utils.helper.ObremponProcessHelper
import com.obrempon.credit.market.utils.helper.ObremponProductHelper
import com.obrempon.credit.market.utils.shared.ObremponSharedHelper
import org.json.JSONObject

/**
 * ObremponCredit
 * @Author wwdeng
 * @CreateTime: 2024/12/16 13:41
 * @Annotation：额度审核被拒绝
 */
class ObremponCreditRejectedAct: CommonActivity() {
    lateinit var binding: ObremponCreditRejectedActBinding
    private var deleteDataAfterDate = ""

    override fun takeObremponRootView(): View {
        binding = ObremponCreditRejectedActBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initObremponView() {
        setObremponTitleBarVisibility(View.VISIBLE)
        setObremponBackBtnRes(R.mipmap.obrempon_back_white_icon)
        setObremponRightBtnRes(R.mipmap.obrempon_customer_icon)
        setObremponTextTitleVisibility(View.GONE)
        rootViewBinding.rootLayout.setBackgroundColor(getColor(R.color.color_ff0569ff))
    }

    override fun initObremponListener() {
        rootViewBinding.rootCustomer.setOnClickListener {
            showCustomerDialog("Limit Rejected Page")
            addEventPointRequestAction(ObremponEventPointType.CLICK.OBREMPON_CLICK_CUSTOMER,"Limit Rejected Page")
        }
        //删除用户数据
        binding.buttonDeleteData.setOnClickListener {
            addEventPointRequestAction(ObremponEventPointType.CLICK.OBREMPON_CLICK_USER_DATA_CLEAR,"Limit Rejected Page")
            val pList = ObremponProductHelper.takeObremponUncompleteProductList()
            //存在未结清的账单
            if (pList.size > 0){
                showErrorTipsDialog(getString(R.string.obrempon_user_data_clear_error),"Limit Rejected Page")
                return@setOnClickListener
            }
            showObremponClearDataDialog()
        }
    }

    override fun takeObremponData() {
        val againApplyDate = ObremponProcessHelper.takeObremponAuthApplyAgainDate()
        val showTips = String.format(getString(R.string.obrempon_order_details_rejected_tips), againApplyDate)
        val index = showTips.indexOf(againApplyDate)
        val spString = SpannableString(showTips)
        spString.setSpan(ForegroundColorSpan(getColor(R.color.color_ffff8f28)), index, index + againApplyDate.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        //加粗
        spString.setSpan(StyleSpan(Typeface.BOLD), index, index + againApplyDate.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        binding.textLimitRejectedApplyAgainDate.text = spString

        binding.buttonDeleteData.visibility = View.INVISIBLE
        //需要判断是否需要显示删除用户数据按钮
        submitObremponUserClearDataAction(true)
    }

    /**
     * 清空用户数据dialog
     */
    private fun showObremponClearDataDialog(){
        val clearDialog = ObremponUserDataClearDialog(this,object : ObremponUserDataClearDialog.IDialogCallback{
            override fun onUserDataClearConfirmClick() {
                //删除用户数据
                submitObremponUserClearDataAction(false)
            }
        })
        clearDialog.setCancelable(true)
        clearDialog.setCanceledOnTouchOutside(true)
        clearDialog.show()
        clearDialog.showObremponDeletePersonalDataDesc(ObremponConfigsHelper.takeObremponDeletePersonalDataDesc(),deleteDataAfterDate)
    }

    /**
     * 清除用户数据 或 获取是否显示删除数据按钮
     */
    private fun submitObremponUserClearDataAction(isShowDeleteButton: Boolean){
        showObremponLoading()
        val httpUrl = ObremponApiUrl.queryEasyHttpUrl()
        val params = JSONObject().addCommonParams().apply {
            put("path", ObremponApiPath.OBREMPON_PATH.OBREMPON_API_USER_DATA_CLEAR_PATH)
        }
        if (isShowDeleteButton){
            params.put("type","GET")
        } else {
            params.put("type","DROP")
        }
        doPostAction(httpUrl, params, object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                if (isShowDeleteButton){
                    val obj = data.optJSONObject("obj")
                    if (obj != null && obj.has("showDropButton") && obj.optString("showDropButton") == "YES"){
                        binding.buttonDeleteData.visibility = View.VISIBLE
                        deleteDataAfterDate = obj.optString("againDate")
                    } else {
                        binding.buttonDeleteData.visibility = View.INVISIBLE
                    }
                } else {
                    hideObremponLoading()
                    //退出登录，需要移除用户保存的信息
                    ObremponSharedHelper.onObremponLoginOut()
                    onIntent(ObremponLoginAct::class.java)
                }
            }
            override fun onRequestError(error: JSONObject) {
                showObremponToast(error.optString("errorMsg"))
            }
            override fun onRequestFinish() {
                hideObremponLoading()
            }
        })
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(ObremponEventPointType.PAGE.OBREMPON_PAGE_CREDIT_REJECTED_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        addEventPointRequestAction(ObremponEventPointType.PAGE.OBREMPON_PAGE_CREDIT_REJECTED_END)
    }
}